<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * TimeSheet
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Tuan NT
 * @link		http://bungtay.com/hrm3/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Timesheet extends REST_Controller{
    
    function __construct(){
        parent::__construct();
        //$this->load->model('user','',TRUE);
        $this->load->model(array('timesheet_model','user_model'));

    }
    
    /**
     * 
     * Get one TimeSheet by Id
     * @author TuanNt
     * @link http://localhost/hrm/api/timesheet/timesheetById/id/$id/format/json
     * @param Integer $id
     * @return Json one TimeSheet
     * 
     **/
    function byid_get(){
        $token = $this->get('token');
        $token_secret = $this->get('token_secret');

        $this->oauth($token);

        $id = $this->get('id');
        if(!$id){
        	$this->response(NULL, 400);
        }

        $oneTimeSheet = $this->timesheet_model->getitem(array('idtimesheet'=>$id));
        
        if(!empty($oneTimeSheet)){
           $this->response(array('result'=>$oneTimeSheet)); 
        }else{
            $this->response(array('error' => 'TimeSheet could not be found'), 404);
        }
    }
    
    /**
     * 
     * Get all List TimeSheet
     * @author TuanNt
     * @link http://localhost/hrm/api/timesheet/timesheets/format/json
     * @return Json List TimeSheet
     * 
     **/
    function list_get(){
        $token = $this->get('token');
        $token_secret = $this->get('token_secret');

        $this->oauth($token);

        $limit = $this->get('limit');
        $offset = $this->get('offset');

        $timesheets = $this->timesheet_model->getlist($limit,$offset);
        if($timesheets){
            $this->response(array('result'=>$timesheets));
        }else{
            $this->response(array('error' => 'No have any timesheet'), 404);
        }
    }
    
    /**
     * 
     * Update one TimeSheet
     * @author TuanNt
     * @link http://localhost/hrm/api/timesheet/timesheetEdit/
     * @return Json
     * 
     **/
    function add_post(){
        $params = json_decode(file_get_contents('php://input'), TRUE);

            $token = $this->get('token');

            

            $this->oauth($token);

            // $staff = $params['staff'];
            // $staff = explode('--',$staff);
            $staffid = $params['staff'];
            
            $datestart = $params['datestart'];
            // $datestart = str_replace('/','-',$datestart) ;
            // $datestart = strtotime($datestart);
            
            $dateend = $params['dateend'];
            // $dateend = str_replace('/','-',$dateend) ;
            // $dateend = strtotime($dateend);
            
            $param['name'] = $params['name'];
            $param['datestart'] = $datestart;
            $param['dateend'] = $dateend;
            $param['detail'] = $params['detail'];
            $param['active'] =  1;
            $param['idstaff'] = $staffid;
            $param['key'] = $params['hours'];    
            $param["datecreated"] = time();
            $param["dateupdated"] = time();
            
            if($this->timesheet_model->additem($param)){
                $this->response(array('result'=>'true'), 200);    
            }else{
                $this->response(array('result'=>'false'), 200);    
            }
    }

    /**
     * 
     * Update one TimeSheet
     * @author TuanNt
     * @link http://localhost/hrm/api/timesheet/timesheetEdit/
     * @return Json
     * 
     **/
    function update_post(){
        $params = json_decode(file_get_contents('php://input'), TRUE);

            $token = $this->get('token');

            

            $this->oauth($token);

            $staffid = $params['staff'];
            
            $datestart = $params['datestart'];
            // $datestart = str_replace('/','-',$datestart) ;
            // $datestart = strtotime($datestart);
            
            $dateend = $params['dateend'];
            // $dateend = str_replace('/','-',$dateend) ;
            // $dateend = strtotime($dateend);
            
            $param['name'] = $params['name'];
            $param['datestart'] = $datestart;
            $param['dateend'] = $dateend;
            $param['detail'] = $params['detail'];
            $param['idstaff'] = $staffid;
            $param['key'] = $params['hours'];    
            $param["dateupdated"] = time();

            $condition = array('idtimesheet'=>$params['id']);
            
            
            if($this->timesheet_model->updateitem($param,$condition)){
                $this->response(array('result'=>'true'), 200);    
            }else{
                $this->response(array('result'=>'false'), 200);    
            }
    }

    /**
     * 
     * Update one TimeSheet
     * @author TuanNt
     * @link http://localhost/hrm/api/timesheet/timesheetEdit/
     * @return Json
     * 
     **/
    function delete_post(){
        $params = json_decode(file_get_contents('php://input'), TRUE);

            $token = $this->get('token');

            

            $this->oauth($token);

            if($this->timesheet_model->deleteitem(array('idtimesheet'=>$params['id']))){
                $this->response(array('result'=>'true'), 200);    
            }else{
                $this->response(array('result'=>'false'), 200);    
            }
    }

    function oauth($token,$token_secret=null){
        //token=ab3cd9j4ks73hf7g&token_secret=xyz4992k83j47x0b
        $oauth = $this->user_model->auth_token($token);
        if($oauth){
            if(time()<$oauth['date_created']){
                    $cnttime = time()+10800;
                        
                    $data = array('date_created'=>$cnttime);
                    $cont = array('id' => $oauth['id']);
                    $xs = $this->user_model->updateKey($data,$cont);
                    //$this->response($xs, 200);
            }else{
                $this->response(array('error' => 'Wrong token'), 401);
            }
        }else{
            $this->response(array('error' => 'Wrong token'), 401);
        }
    }
}

?>